package cn.add;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.os.BatteryManager;
import android.os.Handler;
import android.service.wallpaper.WallpaperService;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.SurfaceHolder;
import cn.add.cache.CacheManage;
import cn.add.model.Battery;
import cn.add.model.Signal;
import cn.add.ui.Panel;

public class LiveWallpaper1 extends WallpaperService{

	@Override
	public void onDestroy() {
		if(tm!=null){
			tm = null;
		}
		super.onDestroy();
	}

	private MyEngine mEngine;
	private Battery mBattery;
	private Signal mSignal = new Signal();
	TelephonyManager tm ;
	private static final String TAG = "defoe";
	
	@Override
	public Engine onCreateEngine() {
		mBattery = new Battery();
		mEngine = new MyEngine(mBattery,mSignal);
		
		tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
		register();
		return mEngine;
	}
	
	@Override
	public void onCreate() {
		super.onCreate();
	}
	
	private void register(){
		IntentFilter mFilter = new IntentFilter();
		mFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
		registerReceiver(new BatteryReceiver(mEngine),mFilter);
	}
	
	private class BatteryReceiver extends BroadcastReceiver{
		
		private MyEngine mEngine;
		
		public BatteryReceiver(MyEngine engine){
			mEngine = engine;
		}
		
		@Override
		public void onReceive(Context context, Intent intent) {
			String action = intent.getAction();
			if(action.equals(Intent.ACTION_BATTERY_CHANGED)){
				//电池电量 数字
			    Log.d(TAG, "level = " + intent.getIntExtra("level", 0));
			    if(mEngine!=null){
			    	mEngine.getBattery().setMLevel(intent.getIntExtra("level", 0));
			    	mEngine.getBattery().setMStatus(intent.getIntExtra("status", BatteryManager.BATTERY_STATUS_UNKNOWN));
			    }
			    //电池最大容量
			    Log.d(TAG, "scale = " + intent.getIntExtra("scale", 0));   
			    //电池的伏数
			    Log.d(TAG, "voltage = " + intent.getIntExtra("voltage", 0));   
			    //电池的温度
			    Log.d(TAG, "temperature = " + intent.getIntExtra("temperature", 0));
                //BatteryManager.BATTERY_STATUS_CHARGING 充电状态
                //BatteryManager.BATTERY_STATUS_DISCHARGING 放电中
                //BatteryManager.BATTERY_STATUS_NOT_CHARGING 未充电
                //BatteryManager.BATTERY_STATUS_FULL 电池满
			    //电池状态
			    Log.d(TAG, "status = " + intent.getIntExtra("status", BatteryManager.BATTERY_STATUS_UNKNOWN));
			    //BatteryManager.BATTERY_PLUGGED_AC 如果是这个值表示充电器 不是则为USB
			    //充电类型
			    Log.d(TAG, "plugged = " + intent.getIntExtra("plugged", 0));
	            //BatteryManager.BATTERY_HEALTH_GOOD 良好
	            //BatteryManager.BATTERY_HEALTH_OVERHEAT 过热
	            //BatteryManager.BATTERY_HEALTH_DEAD 没电
	            //BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE 过电压
	            //BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE 未知错误
			    //电池健康状态
			    Log.d(TAG, "health = " + intent.getIntExtra("health", BatteryManager.BATTERY_HEALTH_UNKNOWN));
			}
		}
		
	}

	private class MyEngine extends Engine{
		private PhoneStateListener mPhoneStateListener = new PhoneStateListener(){
			@Override
			public void onSignalStrengthChanged(int asu) {
				super.onSignalStrengthChanged(asu);
				if(mEngine!=null){
			    	mEngine.getSignal().setModel(0);
			    	mEngine.getSignal().setASU(asu);
//			    	mEngine.getBattery().setMStatus(intent.getIntExtra("status", BatteryManager.BATTERY_STATUS_UNKNOWN));
			    }
			}
		};
		private Battery mBattery;
		
		//private Bitmap mBG1 = getBitmap(R.drawable.starlight_background);
		
		private Handler handler = new Handler();
		
		private Panel mPanel;
		
		private Runnable mDrawRunner = new Runnable(){
			@Override
			public void run() {
				drawText();
			}
		};
		
		public MyEngine(){
		}

		public MyEngine(Battery battery,Signal signal){
			this();
			mBattery = battery;
			mSignal = signal;
			CacheManage.init(LiveWallpaper1.this);
			mPanel = new Panel(LiveWallpaper1.this.getApplicationContext(),mBattery,mSignal);
		}
		
		public Battery getBattery(){
			return mBattery;
		}
		
		public Signal getSignal(){
			return mSignal;
		}
		
		@Override
		public void onCreate(SurfaceHolder surfaceHolder) {
			super.onCreate(surfaceHolder);
			tm.listen(mPhoneStateListener, PhoneStateListener.LISTEN_SIGNAL_STRENGTH);
		}

		private Bitmap getBitmap(int resId){
			BitmapDrawable mDrawable = (BitmapDrawable)getResources().getDrawable(resId);
			return mDrawable.getBitmap();
		}
		
		@Override
		public void onOffsetsChanged(float offset, float offset2,
				float offsetStep, float offsetStep2, int pixelOffset,
				int pixelOffset2) {
			super.onOffsetsChanged(offset, offset2, offsetStep, offsetStep2, pixelOffset,pixelOffset2);
		}

		@Override
		public void onVisibilityChanged(boolean visible) {
			if(visible){
				drawText();
			}else{
				handler.removeCallbacks(mDrawRunner);
			}
		}
		
		private void drawText(){
			final SurfaceHolder mHolder = getSurfaceHolder();
			Canvas mCanvas = mHolder.lockCanvas();
			if(mCanvas!=null){
				//mCanvas.drawBitmap(mBG1, 0, 0, null);
				drawPanel(mCanvas);
				mHolder.unlockCanvasAndPost(mCanvas);
				handler.removeCallbacks(mDrawRunner);
				handler.postDelayed(mDrawRunner, 100);
			}
		}
		
		private void drawPanel(Canvas mCanvas){
			mPanel.draw(mCanvas);
		}
		
	}
	
}
